//
//  AdDelegate.m
//  CandyJump
//
//  Created by Jordy on 11年4月3日.
//  Copyright 2011 Peru Republic. All rights reserved.
//

#import "AdDelegate.h"
#import "AdMobView.h"

@implementation AdDelegate

- (id) init
{
	if ((self = [super init]))
	{
		viewController = [[UIViewController alloc] init];
		viewController.view = [[CCDirector sharedDirector] openGLView];
		
		adMobVisible = NO;
	}
	return self;
}

- (void) dealloc
{
	[viewController release];
	[super dealloc];
}

#pragma mark -
#pragma mark AdMobDelegate methods
//---------------------------------------------------------------------------------------------------------------------
// Hide the given AdMob banner with animation
- (void) hideAdMobView:(AdMobView*)adView
{
	@synchronized(self)
	{
		if (adMobVisible == YES)
		{
			[UIView beginAnimations:@"animateBannerOff" context:NULL];
			adView.frame = CGRectOffset(adView.frame, 0, 48);
			[UIView commitAnimations];
			
			adMobVisible = NO;
		}
	}
}

// Show the given AdMob banner with animation
- (void) showAdMobView:(AdMobView*)adView
{
	@synchronized(self)
	{
		if (adMobVisible == NO)
		{
			[UIView beginAnimations:@"animateBannerOn" context:NULL];
			adView.frame = CGRectOffset(adView.frame, 0, -48);
			[UIView commitAnimations];
			
			adMobVisible = YES;
		}
	}
}

- (NSString *)publisherIdForAd: (AdMobView *)adView
{
	return @"a14d7f862fcddc4";
}

- (UIViewController *)currentViewControllerForAd:(AdMobView *)adView
{
	return viewController;
}

- (NSArray *)testDevices
{
	return [NSArray arrayWithObjects:ADMOB_SIMULATOR_ID, nil];
}

- (void)didReceiveAd:(AdMobView *)adView
{
	[self showAdMobView:adView];
}

- (void)didReceiveRefreshedAd:(AdMobView *)adView
{
	[self showAdMobView:adView];
}

- (void)didFailToReceiveAd:(AdMobView *)adView
{
	[self hideAdMobView:adView];
}

@end
